/*
  Tên:Trần Hoàng Lâm
  MSSV:11379951
  Lớp:DHTH7DLT
  Ngày hoàn thành:24/08/2012
  */
#include "frmkhachhang.h"
#include "ui_frmkhachhang.h"
#include "quanlybansach.h"
frmKhachHang::frmKhachHang(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::frmKhachHang)
{
    ui->setupUi(this);
    //Khởi tạo
    trangThai=false;
    DB =new MySqlConnection();
    resetTableSelectionEvent();
    //Gán sự kiện
    this->connect(ui->btnThem,SIGNAL(clicked()),this,SLOT(themKhachHang()));
    this->connect(ui->btnSua,SIGNAL(clicked()),this,SLOT(suaKhachHang()));
    this->connect(ui->btnXoa,SIGNAL(clicked()),this,SLOT(xoaKhachHang()));
    this->connect(ui->btnThoat,SIGNAL(clicked()),this,SLOT(Thoat()));
    this->connect(ui->btnTim,SIGNAL(clicked()),this,SLOT(timKiem()));
    ui->tbviewKH->selectRow(0);
    doiButton(true);
}

frmKhachHang::~frmKhachHang()
{
    delete ui;
}

void frmKhachHang::themKhachHang()
{
    doiButton(false);
    ui->txtTenKH->clear();
    ui->txtDiaChi->clear();
    ui->txtDienThoai->clear();
    ui->txtEmail->clear();
    //Khi thêm trạng thái =true
    trangThai=true;
}

void frmKhachHang::suaKhachHang()
{
    //Khi thêm
    bool tl=true;
    if(ui->btnSua->text()==trUtf8("Sửa"))
    {
        doiButton(false);
        return;
    }
    if(ui->txtTenKH->text().trimmed().isEmpty())
    {
        QMessageBox::information(this,trUtf8("Thông báo"),trUtf8("Vui lòng nhập tên khách hàng"));
        return;
    }
    if(ui->txtDiaChi->text().trimmed().isEmpty())
    {
        QMessageBox::information(this,trUtf8("Thông báo"),trUtf8("Vui lòng nhập tên địa chỉ khách hàng"));
        return;
    }
    if(trangThai)
    {

        if(DB->KiemTraTrung("h2l_KhachHang","TenKH",ui->txtTenKH->text().trimmed()))
        {
            QMessageBox::warning(this,trUtf8("Thông báo"),trUtf8("Tên khách hàng này đã có trong danh sách"));
            ui->txtTenKH->setFocus();
            ui->txtTenKH->selectAll();
            return;
        }

        DB->Open();
        QSqlQuery query(DB->DB);
        query.prepare("INSERT INTO h2l_KhachHang (TenKH,Phone,Email,DiaChi) VALUES (:tenKH,:phone,:email,:diachi)");
        query.bindValue(":tenKH",ui->txtTenKH->text().trimmed());
        query.bindValue(":phone",ui->txtDienThoai->text().trimmed());
        query.bindValue(":email",ui->txtEmail->text().trimmed());
        query.bindValue(":diachi",ui->txtDiaChi->text().trimmed());
        tl= query.exec();
        DB->Close();
        if(!tl)
            QMessageBox::information(this,trUtf8("Có lỗi xảy ra"),query.lastError().text());
        else
            resetTableSelectionEvent();

        //Gán lại trạng thái
        trangThai=false;

    }
    else
    {
        //Sửa tác giả

        DB->Open();
        QSqlQuery query(DB->DB);
        query.prepare("UPDATE h2l_KhachHang set TenKH=:tenKH,Phone=:phone,Email=:email,DiaChi=:diachi WHERE maKH=:maKH");
        query.bindValue(":tenKH",ui->txtTenKH->text().trimmed());
        query.bindValue(":phone",ui->txtDienThoai->text().trimmed());
        query.bindValue(":email",ui->txtEmail->text().trimmed());
        query.bindValue(":diachi",ui->txtDiaChi->text().trimmed());
        query.bindValue(":maKH",m_MaKH);
        tl= query.exec();
        DB->Close();
        if(!tl)
            QMessageBox::information(this,trUtf8("Có lỗi xảy ra"),query.lastError().text());
        else
            {
            resetTableSelectionEvent();
            ui->tbviewKH->selectRow(m_rowIndex);
            }
    }

    doiButton(true);

}

void frmKhachHang::xoaKhachHang()
{
    if(ui->btnXoa->text()==trUtf8("Không lưu"))
    {
        doiButton(true);
        selectRow();
        return;
    }

     bool tl=true;
     QString msg=trUtf8("Bạn muốn xóa khách hàng '")+ui->txtTenKH->text()+trUtf8("'\nra khỏi danh sách không ?");
    if(QMessageBox::information(this,trUtf8("Thông báo"),msg,QMessageBox::Yes,QMessageBox::No)==QMessageBox::Yes)
    {
        DB->Open();
        QSqlQuery query(DB->DB);
        query.prepare("DELETE FROM h2l_KhachHang WHERE maKH=:maKH");
        query.bindValue(":maKH",m_MaKH);
        tl= query.exec();
        DB->Close();
        if(!tl)
            QMessageBox::information(this,trUtf8("Có lỗi xảy ra"),query.lastError().text());
        else
        {
            resetTableSelectionEvent();
            if(m_MaKH>0)
                m_rowIndex--;
            ui->tbviewKH->selectRow(m_rowIndex);
            selectRow();
        }
    }

}

void frmKhachHang::Thoat()
{
    close();
    QuanLyBanSach *sach=qobject_cast<QuanLyBanSach*>(parent());
    sach->layThongTinKH(m_MaKH);
    sach->timkhachhang();
}

void frmKhachHang::doiButton(bool b)
{
    ui->btnThem->setEnabled(b);
    ui->tbviewKH->setEnabled(b);
    ui->txtTenKH->setReadOnly(b);
    ui->txtDiaChi->setReadOnly(b);
    ui->txtDienThoai->setReadOnly(b);
    ui->txtEmail->setReadOnly(b);

    if(!b)
    {
        ui->btnSua->setText(trUtf8("Lưu"));
        ui->btnXoa->setText(trUtf8("Không lưu"));
    }
    else

    {
        ui->btnSua->setText(trUtf8("Sửa"));
        ui->btnXoa->setText(trUtf8("Xóa"));

    }
}

void frmKhachHang::selectRow()
{
    QAbstractItemModel *item =ui->tbviewKH->model();
    QModelIndex index = ui->tbviewKH->currentIndex();
    m_rowIndex = index.row();
    ui->txtTenKH->setText(item->data(item->index(m_rowIndex, 1)).toString());
    ui->txtDienThoai->setText(item->data(item->index(m_rowIndex, 3)).toString());
    ui->txtEmail->setText(item->data(item->index(m_rowIndex, 4)).toString());
    ui->txtDiaChi->setText(item->data(item->index(m_rowIndex, 2)).toString());
    m_MaKH=item->data(item->index(m_rowIndex,0)).toInt();
}

void frmKhachHang::timKiem()
{
    if(ui->btnTim->text()==trUtf8("Tìm"))
    {
        QString sql="select * from h2l_KhachHang where  maKH like '%"+ui->txtTim->text().trimmed()+"%' or TenKH like '%"+ui->txtTim->text().trimmed()+"%'";
        QSqlQueryModel *model = DB->getTable(sql);
        model->setHeaderData(0, Qt::Horizontal,trUtf8("Mã"));
        model->setHeaderData(1, Qt::Horizontal,trUtf8("Tên KH"));
        model->setHeaderData(2, Qt::Horizontal,trUtf8("Địa Chỉ"));
        model->setHeaderData(3, Qt::Horizontal,trUtf8("Điện Thoại"));
        model->setHeaderData(4, Qt::Horizontal,trUtf8("Email"));
        ui->tbviewKH->setModel(model);
        ui->tbviewKH->show();
        if(!ui->txtTim->text().trimmed().isEmpty())
        ui->btnTim->setText(trUtf8("Thôi"));
    }
    else
    {
        loadDS();
        ui->btnTim->setText(trUtf8("Tìm"));
        ui->txtTim->clear();
    }
}

void frmKhachHang::resetTableSelectionEvent()
{
    loadDS();
    selectionModel = ui->tbviewKH->selectionModel();
    connect(selectionModel, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), this, SLOT(selectRow()));
}

void frmKhachHang::loadDS()
{
    QSqlQueryModel *model = DB->getTable("select * from h2l_KhachHang");
    model->setHeaderData(0, Qt::Horizontal,trUtf8("Mã"));
    model->setHeaderData(1, Qt::Horizontal,trUtf8("Tên KH"));
    model->setHeaderData(2, Qt::Horizontal,trUtf8("Địa Chỉ"));
    model->setHeaderData(3, Qt::Horizontal,trUtf8("Điện Thoại"));
    model->setHeaderData(4, Qt::Horizontal,trUtf8("Email"));
    ui->tbviewKH->setModel(model);
    ui->tbviewKH->setColumnWidth(0,40);
    ui->tbviewKH->setColumnWidth(1,250);
    ui->tbviewKH->setColumnWidth(2,200);
    ui->tbviewKH->setColumnWidth(3,100);
    ui->tbviewKH->setColumnWidth(4,100);
    ui->tbviewKH->show();
}


